package com.hantanger.weblog.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.hantanger.weblog.dao.SettingDao;
import com.hantanger.weblog.dao.UserDao;
import com.hantanger.weblog.freemarker.StringMerger;
import com.hantanger.weblog.model.Archive;
import com.hantanger.weblog.model.Category;
import com.hantanger.weblog.model.Setting;
import com.hantanger.weblog.model.Tag;
import com.hantanger.weblog.service.PostService;
import com.hantanger.weblog.util.ClassFactory;

public abstract class BaseController{
	
	protected final String JSON = "application/json; charset=UTF-8";
	
	protected final String HTML = "text/html; charset=UTF-8";
	
	protected final String JS = "text/javascript";
	
	protected final String XML = "text/xml;charset=utf-8";
	
	protected final String ATOM = "application/atom+xml;charset=utf-8";

	protected PostService getPostService() {
		return ClassFactory.getInstance(PostService.class);
	}
	
	protected SettingDao getSettingDao(){
		return ClassFactory.getInstance(SettingDao.class);
	}
	
	protected String getSpaceUrl(){
		return getSettingDao().getDefaultSetting().getSpaceUrl();
	}
	
	protected StringMerger getStringMerger(){
		return ClassFactory.getInstance(StringMerger.class);
	}
	
	protected UserDao getUserDao(){
		return ClassFactory.getInstance(UserDao.class);
	}
	
	/**
	 * 返回为null，则自己处理响应
	 * @param request
	 * @param response
	 * @return
	 */
	public String process(HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("blogLink", getSpaceUrl());
		Setting defaultSetting = getSettingDao().getDefaultSetting();
		request.setAttribute("setting", defaultSetting);
		if(request.getMethod().equalsIgnoreCase("GET")){
			fillSideBarData(request);
			String result = doGet(request, response);
			setSEO(request);
			return result;
		}else{
			return doPost(request, response);
		}
	}
	
	protected abstract String doGet(HttpServletRequest request,
			HttpServletResponse response);
	
	protected String doPost(HttpServletRequest request,
			HttpServletResponse response){
		return doGet(request, response);
	}
	
	protected void redirect(String targetUrl, HttpServletRequest request, HttpServletResponse response){
		try {
			response.sendRedirect(targetUrl);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	protected void fillSideBarData(HttpServletRequest request){
		List<Category> categorys = getPostService().getAllCategorys();
		request.setAttribute("categorys", categorys);
		int draftCount = 0;
		if(categorys != null){
			for(Category cat : categorys){
				draftCount += cat.getAllCount() - cat.getPublishedCount();
			}
			request.setAttribute("draftCount", draftCount);
		}
		List<Archive> archives = getPostService().getAllArchives();
		request.setAttribute("archives", archives);
		List<Tag> tags = getPostService().getAllTags();
		request.setAttribute("tags", tags);
	}
	
	protected void render(String desTemplateName,HttpServletRequest request,
			HttpServletResponse response){
		RequestDispatcher dispatcher = request.getRequestDispatcher(desTemplateName);
		try {
			dispatcher.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 返回json格式的数据
	 * @param Object
	 * @param response
	 */
	protected void renderJson(Object data, HttpServletResponse response, String contentType){
		response.setContentType(contentType);
		Gson gson = new Gson();
		String result = gson.toJson(data);
		try {
			PrintWriter writer = response.getWriter();
			writer.write(result);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 设置seo值
	 * @param response
	 */
	protected void setSEO(HttpServletRequest request){
	}
	
}
